package com.itzhigui.controller;

import com.itzhigui.entity.Student;
import com.itzhigui.service.StudentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author java1412
 * @since 2024-04-12 09:47:28
 */
@RestController
@RequestMapping(value = "/student")
public class StudentController {

    @Resource
    private StudentService studentService;

    // 查询所有——get——/student
    // 根据id查询——get——/student/1
    // 添加——post——/student
    // 更新——put——/student
    // 根据id删除——delete——/student/1
    @GetMapping
    public Object list() {
        return studentService.findAll();
    }

    @GetMapping(value = "/{id}")
    public Object list(
            @PathVariable("id") Integer id
    ) {
        return studentService.findById(id);
    }

    @PostMapping
    public Object save(
            // 接收json形式数据，从浏览器提交过来的json格式数据
            @RequestBody Student student
    ) {
        return studentService.add(student);
    }

    @PutMapping
    public Object update(
            // 接收json形式数据，从浏览器提交过来的json格式数据
            @RequestBody Student student
    ) {
        return studentService.update(student);
    }

    @DeleteMapping(value = "/{id}")
    public Object deleteById(
            @PathVariable("id") Integer id
    ) {
        return studentService.deleteById(id);
    }
}
